/*
  Copyright © 2010  Vinícius dos Santos Oliveira

      This file is part of Touché.

      Touché is free software: you can redistribute it and/or modify
      it under the terms of the GNU Lesser General Public License as published
      by the Free Software Foundation, either version 3 of the License, or
      (at your option) any later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU Lesser General Public License for more details.

      You should have received a copy of the GNU Lesser General Public License
      along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */

#ifndef AUDIOELEMENT_H
#define AUDIOELEMENT_H

#include "toucheelement.h"
#ifndef TOUCHE_SHOULD_SKIP_THIS
#include <gst/gst.h>
#endif

#include <stdint.h>
#include <QGraphicsSvgItem>

#include <QTimer>

#ifndef TOUCHE_SHOULD_SKIP_THIS

class AudioRecorder: public QObject
{
    Q_OBJECT

    GstElement *pipeline;
    GstElement *sink;

    bool m_recording;

public:
    AudioRecorder();
    ~AudioRecorder();

    bool recording();

public slots:
    void start(const QString &file);
    void stop();

signals:
    void stopped();
    void mediaChanged();
};

class AudioPlayer: public QObject
{
    friend void on_pad_added(GstElement *element, GstPad *source_pad, gpointer data);

    Q_OBJECT

    GstElement *pipeline;
    GstElement *source;

    bool m_playing;

    QTimer timer;

public:
    AudioPlayer();
    ~AudioPlayer();

    bool playing();

public slots:
    void start(const QString &file);
    void stop();

signals:
    void stopped();
    void mediaChanged();
};

class LineEditItem: public QGraphicsTextItem
{
    Q_OBJECT

protected:
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    void focusOutEvent(QFocusEvent *event);
    void keyPressEvent(QKeyEvent *event);

public:
    LineEditItem(const QString &string);
    QRectF boundingRect() const;

signals:
    void finished(QString);
};

void on_pad_added(GstElement *element, GstPad *source_pad, gpointer data);

#endif

namespace Touche {

    class AudioElement : public Element
    {
        Q_OBJECT

        enum State
        {
            NORMAL,
            RECORDING,
            PLAYING
        } state;

        static quint32 counter;

        bool locked;
        QVector<QString> m_options;
        QString fileName;
        QString description;
        QGraphicsSvgItem *m_item[3];
#ifndef TOUCHE_SHOULD_SKIP_THIS
        LineEditItem *m_descriptionItem;
#else
        QGraphicsTextItem *m_descriptionItem;
#endif

    public:
        AudioElement();
        ~AudioElement();
        const QString &type() const;
        QRectF item(QGraphicsItem *parentItem) const;
        QVector<QString> menuOptions() const;

        QString save() const;
        QString file();
        void load(const QString &xml, const QString &file);

    public slots:
        void onMenuActived(const QString &option);
        void onElementLocked(bool locked);

    private slots:
        void onStopRecording();
        void onStopPlaying();
        void stopEditionMode();
        void showNormal();
        void showRec();
        void showPlay();
//        void hideNormal();
//        void hideRec();
//        void hidePlay();

    signals:
        void playerChanged(AudioElement *);
    };

} // namespace Touche

#endif // AUDIOELEMENT_H
